﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class System_MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Account"] != null)
        {
            this.labLogon.Text = "";
            lab1.Visible = false;

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.DAL.AccountType _AccountType = (HZ.MBSM.DAL.AccountType)SysFun.ToInt(_Account.Type);

            string strPageDescribePath = Server.MapPath("../System/PageDescribe.xml");
            HZ.MBSM.BLL.PageDescribeCollection _PageDescribeCollection = new HZ.MBSM.BLL.PageDescribeCollection();
            _PageDescribeCollection.LoadXML(strPageDescribePath);

            foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in _PageDescribeCollection.ChildrenPageDescribe)
            {
                for (int intIndex = _PageDescribe.ChildrenPageDescribe.Count - 1; intIndex >= 0; intIndex--)
                {
                    bool bolIsChecked = false;

                    foreach (HZ.MBSM.DAL.AccountType _AccountTypePage in _PageDescribe.ChildrenPageDescribe[intIndex].UseAccountType)
                    {
                        if (_AccountTypePage == _AccountType)
                        {
                            bolIsChecked = true;
                            break;
                        }
                    }

                    if (bolIsChecked == false)
                    {
                        _PageDescribe.ChildrenPageDescribe.RemoveAt(intIndex);
                    }
                }
            }

            //     this.labHead.Text = "<table width='1008' border='0' align='center' cellpadding='0' cellspacing='0'><tr><td height='100' align='right' valign='bottom' background='../Images/2_flash_bg.jpg'></td></tr></table>";

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (_Account.Type == "4")
            {
                sb.Append("<table width='232' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr><td><img src='../Images/2_left_title1.jpg' width='232' height='14'></td></tr>");
                sb.Append("<tr><td align='center' background='../Images/left_bg.jpg'>");
                sb.Append("<table width='217' border='0' cellspacing='4' cellpadding='0'>");
                sb.Append("<tr><td>");
                sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr>");
                sb.Append("<td></td>"); //<img src='../Images/logo_JD.gif' with='232'/>
                sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>欢迎 <font color='#0066CC'>" + _Account.DWName + " </font> 登录系统</span></strong></td>");
                sb.Append("</tr>");
                //sb.Append("<tr>");
                //sb.Append("<td class='2_left_bg'><img src='../images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>用户名:<font color='#0066cc'>" + _Account.AccountName + "</font> </span></strong></td>");
                //sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>注册号:<font color='#0066CC'>" + _Account.UserCode + "</font></span></strong> </td>");
                sb.Append("</tr>");
                sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                sb.Append("<a href='../HomePage/Logout.aspx'><strong><span style=‘font-family: 宋体'>◇注销</a>");
                sb.Append("</td></tr>");

                sb.Append("</table>");
                sb.Append("</td></tr>");

                foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in _PageDescribeCollection.ChildrenPageDescribe)
                {
                    if (_PageDescribe.ChildrenPageDescribe.Count > 0)
                    {
                        sb.Append("<tr><td>");
                        sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                        sb.Append("<tr>");
                        sb.Append("<td class='2_left_bg' style='behavior:url(../System/ItemSortPucker.htc)'><img src='../Images/spacer.gif' width='40' height='10'><strong><span style=‘font-family: 宋体'>" + _PageDescribe.Name + "</span></strong></td>");
                        sb.Append("</tr>");
                        sb.Append("<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'>");

                        foreach (HZ.MBSM.BLL.PageDescribe _PageDescribeChildren in _PageDescribe.ChildrenPageDescribe)
                        {
                            sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                            sb.Append("<a href='" + _PageDescribeChildren.Url + "'><strong><span style=‘font-family: 宋体'>◇" + _PageDescribeChildren.Name + "</span></strong></a>");
                            sb.Append("</td></tr>");
                        }

                        sb.Append("</table></td></tr>");
                        sb.Append("</table>");
                        sb.Append("</td></tr>");
                    }
                }

                sb.Append("</table>");
                sb.Append("</td></tr><tr><td><img src='../Images/left_bottom.jpg' width='232' height='14'></td></tr>");
                sb.Append("</table>");
            }
            else
            {
                sb.Append("<table width='232' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr><td><img src='../Images/2_left_title1.jpg' width='232' height='14'></td></tr>");
                sb.Append("<tr><td align='center' background='../Images/left_bg.jpg'>");
                sb.Append("<table width='217' border='0' cellspacing='4' cellpadding='0'>");
                sb.Append("<tr><td>");
                sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'>欢迎 <font color='#0066CC'>" + _Account.DWName + " </font> 登录系统</td>");
                sb.Append("</tr>");
                //sb.Append("<tr>");
                //sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'>用户名:<font color='#0066CC'>" + _Account.AccountName + "</font> </td>");
                //sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("<td class='2_left_bg'><img src='../Images/spacer.gif' width='40' height='10'>注册号:<font color='#0066CC'>" + _Account.UserCode + "</font> </td>");
                sb.Append("</tr>");
                sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                sb.Append("<a href='../HomePage/Logout.aspx'>◇注销</a>");
                sb.Append("</td></tr>");

                sb.Append("</table>");
                sb.Append("</td></tr>");

                foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in _PageDescribeCollection.ChildrenPageDescribe)
                {
                    if (_PageDescribe.ChildrenPageDescribe.Count > 0)
                    {
                        if ((_Account.Type == "1") && (_Account.AccountName.Remove(3, _Account.AccountName.Length - 3) == "Chd") && ((_PageDescribe.Name == "子用户管理") || (_PageDescribe.Name == "用户基本信息")))
                        {
                            continue;
                        }
                        if ((_Account.Type == "2") && (_Account.Sort == "0") && ((_PageDescribe.Name == "日常采购管理")))
                        {
                            continue;
                        }
                        if ((_Account.Type == "2") && (_Account.Sort == "1") && ((_PageDescribe.Name == "交易查询")))
                        {
                            continue;
                        }
                        sb.Append("<tr><td>");
                        sb.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                        sb.Append("<tr>");
                        sb.Append("<td class='2_left_bg' style='behavior:url(../System/ItemSortPucker.htc)'><img src='../Images/spacer.gif' width='40' height='10'>" + _PageDescribe.Name + "</td>");
                        sb.Append("</tr>");
                        sb.Append("<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'>");

                        foreach (HZ.MBSM.BLL.PageDescribe _PageDescribeChildren in _PageDescribe.ChildrenPageDescribe)
                        {
                            if ((_PageDescribeChildren.Name == "用户密码管理") && (_Account.AccountName.Remove(3, _Account.AccountName.Length - 3) == "Chd") && (_Account.Type == "1"))
                            {
                                continue;
                            }
                            if ((_PageDescribeChildren.Name == "增加本企业商品") && (_Account.Type == "2"))
                            {
                                string strProductAddStatus = "select  isnull(ProductAddStatus,0) as ProductAddStatus from Company where  accountid=" + _Account.AccountId;
                                Db db = new Db();
                                DataTable dt = db.DataTable(strProductAddStatus);
                                db.Close();
                                if (dt.Rows[0][0].ToString() == "0")
                                {
                                    continue;
                                }
                            }
                            sb.Append("<tr><td class='2_left_bg2'><img src='../Images/spacer.gif' width='40' height='10'>");
                            if ((_PageDescribeChildren.Name == "日常采购管理") && (_Account.Sort == "0") && (_Account.Type == "2"))
                            {
                                sb.Append("<a href='../Supervise/TraCompanySC.aspx'>◇" + _PageDescribeChildren.Name + "</a>");
                            }
                            else
                            {
                                sb.Append("<a href='" + _PageDescribeChildren.Url + "'>◇" + _PageDescribeChildren.Name + "</a>");
                            }
                            sb.Append("</td></tr>");
                        }

                        sb.Append("</table></td></tr>");
                        sb.Append("</table>");
                        sb.Append("</td></tr>");
                    }
                }

                sb.Append("</table>");
                sb.Append("</td></tr><tr><td><img src='../Images/left_bottom.jpg' width='232' height='14'></td></tr>");
                sb.Append("</table>");
            }
            labPage.Text = sb.ToString();

            //多层树节点


            //Label labNodes = new Label();
            //ReadPageDescribeNode(_PageDescribeCollection.ChildrenPageDescribe, labNodes, 0);
            //labPage.Controls.Add(labNodes);
        }
        else
        {
            lab1.Text = WriteBoard();
            lab1.Visible = true;
        }
    }

    //将PageDescribe树写入到 ListView 中
    private void ReadPageDescribeNode(System.Collections.Generic.List<HZ.MBSM.BLL.PageDescribe> pageDescribes, Label treeNodeCollection, int space)
    {
        foreach (HZ.MBSM.BLL.PageDescribe _PageDescribe in pageDescribes)
        {
            Label labNode = new Label();
            string strSpace = "";
            for (int intIndex = 0; intIndex < space; intIndex++)
            {
                strSpace += "　";
            }
            if (_PageDescribe.Url != null && _PageDescribe.Url.Length > 0)
            {
                labNode.Text = strSpace + "<a href='" + _PageDescribe.Url + "'>" + _PageDescribe.Name + "</a><br>";
            }
            else
            {
                labNode.Text = strSpace + _PageDescribe.Name + "<br>";
            }
            treeNodeCollection.Controls.Add(labNode);

            Label labChildren = new Label();
            treeNodeCollection.Controls.Add(labChildren);

            if (_PageDescribe.ChildrenPageDescribe.Count > 0)
            {
                ReadPageDescribeNode(_PageDescribe.ChildrenPageDescribe, labChildren, space + 1);
            }
        }
    }

    public string Display_Title(object Title, int Len)
    {
        string Title_str = Title.ToString().Trim();
        if (Title_str != "")
        {
            //if(Title_str.Length > Len)
            //	Title_str = Title_str.Substring(0,Len)+"..";
            int ShowNum = WordCount(Title_str, Len);
            if (Title.ToString().Length > ShowNum)
                Title_str = Title.ToString().Substring(0, ShowNum - 1) + "....";
        }
        else
            Title_str = "";

        return Title_str;
    }

    public int WordCount(string Operand, int MaxNum)
    {
        IEnumerator OperandEnum = Operand.GetEnumerator();
        char ch = ' ';
        int i = 0;
        for (int CharCount = 0; OperandEnum.MoveNext() && CharCount <= MaxNum; i++)
        {
            ch = Convert.ToChar(OperandEnum.Current);
            if ((ch >= 32 && ch <= 126) || ch == '=')
                CharCount++;
            else
                CharCount += 2;
        }
        return i;
    }

    public string WriteBoard()
    {
        int i = 0;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 8 id,title from board  order by  id desc ";
        System.Data.IDataReader dr = db.DataReader(strSql);



        sb.Append("<table width='232' border='0' cellspacing='0' cellpadding='0'><tr><td><img src='../Images/left_title4.jpg' width='232' height='56'></td></tr><tr><td background='../Images/left_bg.jpg'><table width='90%' border='0' align='center' cellpadding='4' cellspacing='1'><tr><td >");


        sb.Append("<table  width='96%' border=0 align=center cellpadding=6 cellspacing=1>");
        while (dr.Read())
        {
            i++;
            sb.Append("<tr><td class='dotbottomline'>");
            sb.Append("<img src='../images/ico7.gif' width='8' height='5' hspace='4' vspace='1'>");
            // sb.Append("<a href='../HomePage/Boardinfo.aspx?Id=" + dr["id"].ToString() + "' target=_blank>" + Display_Title(dr["Title"].ToString(), 20) + "</a>");

            sb.Append("</td></tr>");
        }
        for (int j = i; j < 8; j++)
            sb.Append("<tr><td class='dotbottomline'>&nbsp;</td></tr>");

        sb.Append("</table>");
        dr.Close();
        dr.Dispose();
        db.Close();
        sb.Append("</td></tr><tr align=right ><td><a href='../HomePage/BordList.aspx'>更多...</a></td></tr></table></td></tr><tr align=right><td><img src='../Images/left_bottom.jpg' width='232' height='14'></td></tr></table>");
        return sb.ToString();
    }
}
